/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/**
 * File model class
 */
qx.Class.define("webapp.model.FileEditor", {

  extend : webapp.model.AbstractEditor,

  members : {

    _createFormModel : function()
    {
      var values = {
        uploader : null,
        files    : null
      };

      this._formModel = qx.data.marshal.Json.createModel(values);
      this.fireDataEvent("loaded", values);
    },
    
    notifyFileSaved : function(name)
    {
      this.fireDataEvent("saved", name);
    },

    save : function()
    {
      var rpc       = new webapp.network.Rpc(this._serviceName);
      var urlParams = rpc.getCallUrlParameters("upload", []);
      var uploader  = this._formModel.getUploader();
      for(var name in urlParams)
      {
        uploader.setParam(name, urlParams[name]);
      }
      uploader.setUploadUrl(rpc.getUrl());
      uploader.getUploadHandler().beginUploads();
    },
    
    remove : function(name)
    {
      var rpc = new webapp.network.Rpc(this._serviceName);
      rpc.addListener("completed", function()
      {
        this.fireDataEvent("saved", null);
      }, this);
      
      rpc.call("remove", name);
    },
    
    download : function(name)
    {
      var rpc = new webapp.network.Rpc(this._serviceName);
      var url = rpc.getCallUrl("download", [name]);
      var app = qx.core.Init.getApplication(); 
      app.getRoot().getContainerElement().getDomElement().ownerDocument.location = url;
    }

  }

});